<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/1
 * Time: 15:35
 */

namespace app\api\model;


class UserWorkuser extends BaseModel
{
//    protected $table='user_workuser';
    protected $hidden=['update_time','create_time'];
    protected $autoWriteTimestamp = true;

    /**
     * 与user属于一对多关系
     * @return \think\model\relation\BelongsTo
     */
    public function user(){
        return $this->belongsTo('User','user_id','id');
    }

    /**
     * 获取用户信息
     * @param $uid
     * @param $wu_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getUserInfo($uid,$wu_id){
        return self::where('user_id','=',$uid)
            ->where('wu_id','=',$wu_id)
            ->find();
    }

    /**
     * 更新用户信息
     * @param $uid
     * @param $wu_id
     * @param $data
     * @return $this
     */
    public static function updateUserInfo($uid,$wu_id,$data){
        return self::where('user_id','=',$uid)
            ->where('wu_id','=',$wu_id)
            ->update($data);
    }

    /**
     * 获取数量
     * @param $rate
     * @return int|string
     */
    public static function getRateNum($wu_id,$rate1,$rate2){
        return self::where('rate','>=',$rate1)
            ->where('rate','<',$rate2)
            ->where('wu_id','=',$wu_id)
            ->count();
    }

    /**
     * 获取概率数量统计
     * @param $rate
     * @return int|string
     */
    public static function getAllRateNum($t_id,$rate1,$rate2){
        $wu_id = WorkUser::where('t_id','=',$t_id)->column('id');
        return self::where('rate','>',$rate1)
            ->where('rate','<=',$rate2)
            ->where('wu_id','in',$wu_id)
            ->count();
    }

    /**
     * 获取通讯录列表
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getUserList($wu_id){
       $str = self::where('wu_id','=',$wu_id)->order("rate desc")
           ->with(['user'])
           ->field('id,user_id,rate')
            ->select();
       foreach ($str as $item){
           $item->user->visible(['id','avatar_url','nickname']);
       }
      return $str;
    }
    /**
     * 根据ID获取用户
     * @param $ids
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getUserListByIds($ids,$wu_id){
        $str =  self::where('user_id','in',$ids)
            ->where('wu_id','=',$wu_id)
            ->with(['user'])
            ->field('id,user_id,rate')
            ->select();
        foreach ($str as $item){
            $item->user->visible(['id','avatar_url','nickname']);
        }
        return $str;
    }
    /**
     * 更新用户信息
     * @param $data
     * @param $wu_id
     * @param $user_id
     * @return $this
     */
    public static function updateUser($data,$wu_id,$user_id){
        return self::where('wu_id','=',$wu_id)
            ->where('user_id','=',$user_id)->update($data);
    }

    /**
     * 更新授权手机
     * @param $wu_id
     * @param $user_id
     * @param $phone
     * @return $this
     */
    public static function updatePhone($wu_id,$user_id,$phone){
        $data['authorize_phone'] = $phone;
        return self::where('wu_id','=',$wu_id)
                ->where('user_id','=',$user_id)
                ->update($data);
    }

    /**
     * 查询用户是否授权电话
     * @param $user_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function checkAuthPhone($wu_id,$user_id){
        $result = self::where('wu_id','=',$wu_id)
                ->where('user_id','=',$user_id)->field('authorize_phone')->find();
        return $result;
    }

    /**
     * 根据用户id获取用户信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getUserInfoByUid($wu_id,$uid){
       $str =  self::where('user_id','=',$uid)
            ->where('wu_id','=',$wu_id)
            ->with(['user'])
            ->find();
        $str->user->visible(['id','avatar_url','nickname']);
        return $str;
    }

    /**
     * 获取用户id
     * @param $wid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getIDByWid($wid){
        $str  = self::where('wu_id','=',$wid)->field('user_id')->select();
        return $str;
    }

    /**
     * 批量更新
     * @param $uids
     * @param $wid
     * @param $data
     * @return $this
     */
    public static function batchUpdateUser($uids,$wid,$data){
        return self::where('user_id','in',$uids)
            ->where('wu_id','=',$wid)->update($data);
    }

    /**
     * 批量删除
     * @param $uids
     * @param $wid
     * @return int
     */
    public static function deleteUser($uids,$wid){
        return self::where('user_id','in',$uids)
            ->where('wu_id','=',$wid)->delete();
    }

    /**
     * 获取用户数量
     * @param $wid
     * @return int|string
     */
    public static function getUserCount($wid){
        return self::where('wu_id','=',$wid)->count();
    }

    /**
     * 获取用户
     * @param $wid
     * @param $size
     * @return \think\Paginator
     */
    public static function getUserIdListByWuId($wid,$page,$size){
        return self::where('wu_id','=',$wid)
                ->order('create_time desc')
            ->limit(($page-1)*$size,$size)->select();
    }

}